package drds.global_transaction.api.exception;

import drds.global_transaction.api.message_codec.ResponseCode;
import drds.global_transaction.api.message_codec.request_and_response.transaction.TransactionRequest;
import drds.global_transaction.api.message_codec.request_and_response.transaction.TransactionResponse;

public abstract class AbstractExceptionHandler
{

	public interface Callback<T extends TransactionRequest, S extends TransactionResponse>
	{
		void execute(T request, S response) throws GlobalTransactionException;
	}

	public void exceptionHandleTemplate(Callback callback, TransactionRequest request, TransactionResponse response)
	{
		try
		{
			callback.execute(request, response);
			response.setResponseCode(ResponseCode.success);

		} catch (GlobalTransactionException e)
		{
			response.setTransactionExceptionCode(e.getTransactionExceptionCode());
			response.setResponseCode(ResponseCode.failed);
			response.setMessage("GlobalTransactionException[" + e.getMessage() + "]");

		} catch (RuntimeException e1)
		{
			response.setResponseCode(ResponseCode.failed);
			response.setMessage("RuntimeException[" + e1.getMessage() + "]");
		}
	}

}
